//
//  PCDApiAlertinfo.m
//  PCDBank
//
//  Created by lyy on 2021/3/18.
//  Copyright © 2021 DK. All rights reserved.
//

#import "PCDApiAlertinfo.h"
#import "PCDAlertController.h"

@implementation PCDApiAlertinfo

- (void)handler:(id)data andContext:(PCDContext *)context ResponseCallback:(WVJBResponseCallback)responseCallback {
    
    
    NSDictionary *dict=(NSDictionary *)data;
    NSString *confirmText = [dict objectForKey:@"confirmText"] ? [dict objectForKey:@"confirmText"] : @"确定";
    NSString *confirmColor = [dict objectForKey:@"confirmColor"] ? [dict objectForKey:@"confirmColor"] : @"#F63E3D";
    NSString *cancelText= [dict objectForKey:@"cancelText"] ? [dict objectForKey:@"cancelText"] : @"取消";
    NSString *cancelColor = [dict objectForKey:@"cancelColor"] ? [dict objectForKey:@"cancelColor"] :@"#999999";
    
    NSString *content =[PCDUtil safeString:[dict objectForKey:@"content"]];
    NSString *onCancel = [PCDUtil safeString:[dict objectForKey:@"fail"]];
    NSString *onConfirm = [PCDUtil safeString:[dict objectForKey:@"success"]];
    NSString *title =[PCDUtil safeString:[dict objectForKey:@"title"]];
    
    // 是否显示显示按钮，如果true “显示”，如果false，不显示“取消”按钮
    NSString *showCancel =  @"1";
    if ([dict objectForKey:@"showCancel"] != nil && [[dict objectForKey:@"showCancel"] boolValue] == 0) {
        showCancel = @"0";
    }
    
    PCDAlertController *alertController = [PCDAlertController alertControllerWithTitle:title message:content preferredStyle:PCDAlertControllerStyleAlert animationType:PCDAlertAnimationTypeDefault];
    alertController.textAlignment = NSTextAlignmentCenter;
    alertController.minDistanceToEdges = 55;
    alertController.titleColor = [UIColor colorWithRed:34/255.0 green:34/255.0 blue:34/255.0 alpha:1/1.0];
    alertController.titleFont = [UIFont fontWithName:@"PingFangSC-Medium" size:18];
    alertController.messageColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1/1.0];
    alertController.needDialogBlur = NO;
    alertController.tapBackgroundViewDismiss = NO;
	[alertController setBackgroundViewAppearanceStyle:(UIBlurEffectStyleDark) alpha:0.6];
    PCDAlertAction *confirmAction = [PCDAlertAction actionWithTitle:confirmText style:PCDAlertActionStyleDefault handler:^(PCDAlertAction * _Nonnull action) {
        NSDictionary *dic = @{
                               @"confirm":@(YES),
                               @"cancel":@(NO)
                               };
        evaluateJavaScriptOnMainThread(self, onConfirm, [dic JSONRepresentation]);
    }];
    
    confirmAction.titleColor = [UIColor colorFormString:confirmColor];
    if ([showCancel isEqualToString:@"1"]) {
        PCDAlertAction *cancelAction = [PCDAlertAction actionWithTitle:cancelText style:PCDAlertActionStyleDestructive handler:^(PCDAlertAction * _Nonnull action) {
            NSDictionary *dic = @{
                                   @"confirm":@(NO),
                                   @"cancel":@(YES)
                                   };
            evaluateJavaScriptOnMainThread(self, onConfirm, [dic JSONRepresentation]);
        }];
        cancelAction.titleColor = [UIColor colorFormString:cancelColor];
        [alertController addAction:cancelAction];
    }
    [alertController addAction:confirmAction];
    [context.viewController presentViewController:alertController animated:YES completion:nil];
}

@end
